package com.xiaxiang.xiawan.task;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.xiaxiang.xiawan.R;
import com.xiaxiang.xiawan.Util.FileUtil;
import com.xiaxiang.xiawan.Util.HttpDownloader;
import com.xiaxiang.xiawan.Util.XXToolUtil;

import java.io.File;

/**
 * Created by Administrator on 2017-10-30.
 */

public class DownloadEffectTask extends Thread {
    private Handler mHandler;
    public Handler mUiHandler;
    private Activity mActivity;
    public static final int SHOW_DIALOG = 3;
    public static final int START_SHOW = 2;
    public static final int ERROR = 1001;
    public static final int DOWNLOAD_URL = 2001;
    public static final int DOWNLOAD_URL_BY_MD5 = 2002;
    public DownloadEffectTask(Activity activity, Handler uiHandler) {
        mActivity = activity;
        mUiHandler = uiHandler;
    }

    public void run() {
        super.run();
        Looper.prepare();
        mHandler = new Handler(){
            public void handleMessage(Message msg){
                super.handleMessage(msg);
                switch (msg.what){
                    case DOWNLOAD_URL:
                        String url = (String)msg.obj;
                        download(url);
                        break;
                    case DOWNLOAD_URL_BY_MD5:
                        DownloadFile downloadFile = (DownloadFile)msg.obj;
                        downloadByMd5(downloadFile.mUrl,downloadFile.mMd5);
                        break;
                }

            }
        };
        Looper.loop();
    }

    public void startDownload(String url,String fileMd5){
        DownloadFile downloadFile = new DownloadFile();
        downloadFile.mMd5 = fileMd5;
        downloadFile.mUrl = url;
        Message msg = mHandler.obtainMessage();
        msg.obj = downloadFile;
        msg.what = DOWNLOAD_URL_BY_MD5;
        mHandler.sendMessage(msg);
    }
    public void startDownload(String url){
        Message msg = mHandler.obtainMessage();
        msg.obj = url;
        msg.what = DOWNLOAD_URL;
        mHandler.sendMessage(msg);
    }


    private void download(final String fileUrl) {
        Log.e("ur", " download url is " + fileUrl);
        String urlStr = fileUrl;
        String fileName = FileUtil.getNameByUrl(fileUrl);
        File file = new File(FileUtil.getUnity3dDownloadFolder() + fileName);
        String filePath = FileUtil.getUnity3dDownloadFolder() + fileName;
        if (file.exists()) {
            sendMessage(filePath);
            return;
        } else {
            mUiHandler.sendEmptyMessage(SHOW_DIALOG);
        }
        Log.e("ret", "fileName is " + fileName);
        HttpDownloader httpDownloader = new HttpDownloader();
        int ret = httpDownloader.downlaodFile(urlStr, fileName);
        if (ret == 0) {
            sendMessage(filePath);
        } else {
            sendErrorMsg(mActivity.getString(R.string.DownloadMterialError));
        }
    }

    private void downloadByMd5(final String fileUrl,String md5) {
        String urlStr = fileUrl;
        String fileName = FileUtil.getNameByUrl(fileUrl);
        String filePath = FileUtil.getUnity3dDownloadFolder() + fileName;
        File file = new File(filePath);
        Log.e("md5"," fileUrl is "+fileUrl+" md5 is  "+md5);
        Log.e("md5"," filePath is "+filePath+" md5 is  "+md5);
        if (file.exists()) {
            String fileMd5 = XXToolUtil.getFileMD5(filePath);
            Log.e("md5"," fileMd5 is "+fileMd5+" md5 is  "+md5);
            if(fileMd5 != null && fileMd5.equals(md5)){
                sendMessage(filePath);
                return;
            }else{
                file.delete();
                mUiHandler.sendEmptyMessage(SHOW_DIALOG);
            }
        } else {
            mUiHandler.sendEmptyMessage(SHOW_DIALOG);
        }
        Log.e("ret", "fileName is " + fileName);
        HttpDownloader httpDownloader = new HttpDownloader();
        int ret = httpDownloader.downlaodFile(urlStr, fileName);
        if (ret == 0) {
            String fileMd5 = XXToolUtil.getFileMD5(filePath);
            if(fileMd5 != null && fileMd5.equals(md5)){
                sendMessage(filePath);
            }else{
                sendErrorMsg(mActivity.getString(R.string.DownloadMterialM5Error));
            }
        } else {
            sendErrorMsg(mActivity.getString(R.string.DownloadMterialError));
        }
    }

    private void sendMessage(String fileName){
        Message msg = mUiHandler.obtainMessage();
        msg.obj = fileName;
        msg.what = START_SHOW;
        mUiHandler.sendMessage(msg);
    }

    private void sendErrorMsg(String text){
        Message msg = mUiHandler.obtainMessage();
        msg.what = ERROR;
        msg.obj = text;
        mUiHandler.sendMessage(msg);
    }

    public void quitThread(){
        if(mHandler != null){
            mHandler.getLooper().quit();
        }
    }

    private class DownloadFile {
        public String mUrl;
        public String mMd5;
    }
}
